package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.approvetask.entity.CaseApproveRecord;
import com.ruicar.afs.cloud.afscase.approvetask.service.CaseApproveRecordService;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseBaseInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseChannelInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.*;
import com.ruicar.afs.cloud.afscase.risk.service.CaseTortoiseService;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyRentAdjustDetailsService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.BusinessStateInEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ResultBooleanEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ConditionalApprovalReturnInfo;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.risk.api.enums.tortoise.GwtTortoiseAntiFraudCallStep;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @description: 附条件核准
 * @author jiaqi.guo
 * @created 2020/8/14 14:45
 * @version 1.0
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit",name="enable")
public class ConditionalApprovalProcessor implements AfsMqBizProcessor<ConditionalApprovalReturnInfo> {

    private CaseBaseInfoService caseBaseInfoService;
    private CaseTortoiseService caseTortoiseService;
    private CaseApproveRecordService caseApproveRecordService;
    private CaseChannelInfoService caseChannelInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(ConditionalApprovalReturnInfo entity){
        if(ObjectUtils.isNotEmpty(entity)){
            CaseChannelInfo channelInfo = caseChannelInfoService.getOne(
                    Wrappers.<CaseChannelInfo>lambdaQuery()
                        .eq(CaseChannelInfo::getApplyNo,entity.getApplyNo())
            );
            CaseApproveRecord record = new CaseApproveRecord();
            record.setApplyNo(entity.getApplyNo());
            record.setDisposeStaff(channelInfo.getSaleAdvisor());
            record.setApproveEndTime(new Date());
            record.setDisposeNodeName("附条条件审批结果");

            CaseBaseInfo caseBaseInfo=caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda()
            .eq(CaseBaseInfo::getApplyNo,entity.getApplyNo()));
            //进件拒绝是 维护案件状态 取消附条件核准
           if(ResultBooleanEnum.FALSE.getCode().equals(entity.getResult())){
               //取消附條件核准
               caseBaseInfo.setBusinessStateIn(AfsEnumUtil.key(BusinessStateInEnum.CANCEL_ADVICE_CONDITIONAL));
               caseBaseInfoService.updateById(caseBaseInfo);
               record.setApproveSuggestName("拒绝");

           }
           else if(ResultBooleanEnum.TRUE.getCode().equals(entity.getResult())){
               //核准
               caseBaseInfo.setBusinessStateIn(AfsEnumUtil.key(BusinessStateInEnum.CONDITIONAL_APPROVE));
               caseBaseInfoService.updateById(caseBaseInfo);
               log.info("附条件核准进件通过");
               record.setApproveSuggestName("通过");
           }
           // 经销商同意/拒绝附条件核准后触发玄武反欺诈
           caseTortoiseService.safeSignalFraudHand(entity.getApplyNo(), GwtTortoiseAntiFraudCallStep.AFTER_FINALLY_APPROVE_END);
           caseApproveRecordService.save(record);
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
            return MqTransCode.AFS_POS_CASE_APPLY_CTM_APPROVE_CONDITIONAL_NOTICE;
    }
}
